package team.unstudio.unknownrpg_base.attribute;

import java.util.HashMap;
import java.util.Map;

import indi.defoliation.unknownrpg.api.RPGItem;
import indi.defoliation.unknownrpg.api.RPGItemHelper;
import indi.defoliation.unknownrpg.api.attribute.Attribute;
import indi.defoliation.unknownrpg.api.attribute.AttributeBody;
import indi.defoliation.unknownrpg.api.attribute.AttributeEventHandler;
import indi.defoliation.unknownrpg.api.attribute.NumberAttribute;
import indi.defoliation.unknownrpg.api.rpgevent.entity.EntityAttackEvent;

public class DamageAttribute extends Attribute implements NumberAttribute<Double>{

	private double damage=0d;
	
	public DamageAttribute(double damage) {
		this.set(damage);
	}
	
	public Map<String, Object> serialize() {
		HashMap<String,Object> map = new HashMap<>();
		map.put("damage", damage);
		return map;
	}

	@Override
	public void add(Double t) {
		this.damage+=t;
	}

	@Override
	public Double get() {
		return damage;
	}

	@Override
	public void set(Double t) {
		this.damage=t;
	}

	@Override
	public String getKeyName() {
		return "Damage";
	}

	@Override
	public String getDisplayName() {
		return "Damage";
	}

	@Override
	public void deserialize(Map<String, Object> map) {
		this.damage=(double)map.get("damage");
	}
	
	@Override
	public Attribute getAttribute() {
		return this;
	}
	
	@SuppressWarnings("unchecked")
	@AttributeEventHandler(body = AttributeBody.MAIN_HAND)
	public void onAttack(EntityAttackEvent event,Map<AttributeBody,Attribute> map) {
		double damage = event.getChangeDamage()-event.getDamage()+
				event.getFinalDamage()+((DamageAttribute)map.get(AttributeBody.MAIN_HAND)).get();
		if(event.hasLetter("CritChance")) {
			RPGItem rpgItem = RPGItemHelper.getEntityRPGItemsOnBody(event.getEntity(),
					AttributeBody.MAIN_HAND);
			if(rpgItem.hasAttribute("Crit"))
				damage *= ((NumberAttribute<Integer>)rpgItem.getAttribute("Crit")).get();
		}
		event.setDamage(this, damage);
	}
}
